package com.cuitrip.component.emoji;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.lab.utils.DisplayUtil;

/**
 * Created by ak on 16/1/22.
 */
public class NPSEmojiView extends View {

    private Paint mOutLinePaint;

    private Paint mEyePaint;

    private EmotionRate mEmotionRate = EmotionRate.SoSo;


    public enum EmotionRate{
        Pity, SoSo, Happy
    }

    public NPSEmojiView(Context context) {
        super(context);
        init();
    }

    public NPSEmojiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {
        mOutLinePaint = new Paint();
        mOutLinePaint.setAntiAlias(true);
        mOutLinePaint.setColor(ResourceUtil.getColor(R.color.qiaomai_7c706e));
        mOutLinePaint.setStrokeWidth(DisplayUtil.dip2px(6.f));
        mOutLinePaint.setStyle(Paint.Style.STROKE);

        mEyePaint = new Paint();
        mEyePaint.setAntiAlias(true);
        mEyePaint.setColor(ResourceUtil.getColor(R.color.qiaomai_7c706e));
        mEyePaint.setStrokeWidth(DisplayUtil.dip2px(6.f));
        mEyePaint.setStyle(Paint.Style.FILL);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth(); //view的宽度
        int height = getHeight(); //view的高度
        if (width <= 0 || height <= 0) return;

        int eyeOffset = DisplayUtil.dip2px(20.f);
        int eyeRadius = DisplayUtil.dip2px(8.f);

        int mouseOffset = DisplayUtil.dip2px(25.f);

        canvas.save();

        canvas.drawCircle(width / 2, height / 2, height/2 - DisplayUtil.dip2px(6.f) / 2, mOutLinePaint);

        canvas.drawCircle(width/2 - eyeOffset, height/2 - eyeOffset,  eyeRadius, mEyePaint);

        canvas.drawCircle(width/2 + eyeOffset, height/2 - eyeOffset,  eyeRadius, mEyePaint);

        RectF oval = new RectF (width/2 - mouseOffset, height/2, width/2 + mouseOffset, height/2 + mouseOffset);

        if (mEmotionRate.equals(EmotionRate.SoSo)){
            canvas.drawLine(width/2 - mouseOffset, height/2 + mouseOffset/2, width/2 + mouseOffset,  height/2 + mouseOffset/2,mOutLinePaint);
        }else if (mEmotionRate.equals(EmotionRate.Happy)){
            canvas.drawArc(oval, 0, 180, false, mOutLinePaint);
        }else if (mEmotionRate.equals(EmotionRate.Pity)){
            canvas.drawArc(oval, 180, 180, false, mOutLinePaint);
        }
        canvas.restore();

    }


    public void setEmotionRate(EmotionRate emotionRate){
        if (this.mEmotionRate.equals(emotionRate)){
            return;
        }
        this.mEmotionRate = emotionRate;
        postInvalidate();
    }


}
